"""
初始化数据脚本
"""
import asyncio
from app.database import AsyncSessionLocal, create_tables
from app.services.theme_service import theme_service
from app.schemas.theme import ThemeCreate
from app.utils.logger import logger


async def init_sample_data():
    """初始化示例数据"""
    logger.info("开始初始化示例数据...")
    
    async with AsyncSessionLocal() as db:
        try:
            # 检查是否已有主题数据
            existing_themes = await theme_service.get_themes(db, 0, 1)
            if existing_themes.total > 0:
                logger.info("数据库中已存在主题数据，跳过初始化")
                return
            
            # 创建示例主题
            sample_themes = [
                ThemeCreate(
                    name="科技前沿",
                    description="最新的科技动态、人工智能、区块链等前沿技术分享",
                    keywords=["人工智能", "区块链", "科技", "创新", "未来"],
                    style="专业分析",
                    target_audience="科技爱好者",
                    content_length=2000,
                    is_active=True
                ),
                ThemeCreate(
                    name="生活方式",
                    description="健康生活、美食制作、旅行攻略等生活相关内容",
                    keywords=["健康", "美食", "旅行", "生活", "品质"],
                    style="轻松愉快",
                    target_audience="都市白领",
                    content_length=1500,
                    is_active=True
                ),
                ThemeCreate(
                    name="职场成长",
                    description="职业发展、技能提升、管理经验等职场相关话题",
                    keywords=["职场", "成长", "管理", "技能", "发展"],
                    style="实用指导",
                    target_audience="职场人士",
                    content_length=2200,
                    is_active=True
                ),
                ThemeCreate(
                    name="财经资讯",
                    description="股市分析、投资理财、经济趋势等财经内容",
                    keywords=["投资", "理财", "股市", "经济", "财富"],
                    style="专业权威",
                    target_audience="投资者",
                    content_length=1800,
                    is_active=True
                ),
                ThemeCreate(
                    name="教育培训",
                    description="学习方法、技能培训、知识分享等教育相关内容",
                    keywords=["学习", "教育", "培训", "技能", "知识"],
                    style="教学指导",
                    target_audience="学习者",
                    content_length=2000,
                    is_active=True
                )
            ]
            
            # 批量创建主题
            created_count = 0
            for theme_data in sample_themes:
                try:
                    theme = await theme_service.create_theme(db, theme_data)
                    logger.info(f"创建示例主题成功: {theme.name}")
                    created_count += 1
                except Exception as e:
                    logger.error(f"创建示例主题失败: {theme_data.name} - {str(e)}")
            
            logger.info(f"示例数据初始化完成，共创建 {created_count} 个主题")
            
        except Exception as e:
            logger.error(f"初始化示例数据失败: {str(e)}")
            raise


async def main():
    """主函数"""
    try:
        # 首先创建数据库表
        await create_tables()
        logger.info("数据库表检查完成")
        
        # 初始化示例数据
        await init_sample_data()
        
        logger.info("数据初始化完成")
        
    except Exception as e:
        logger.error(f"初始化失败: {str(e)}")
        raise


if __name__ == "__main__":
    asyncio.run(main())